'use strict';
angular.module('app')
    .controller('integralDrawController', ['$rootScope', '$scope', 'ajaxRquest', '$state', '$interval', '$location', function ($rootScope, $scope, ajaxRquest, $state, $interval, $location) {
        document.title = "抽奖";//页面标题
        /*抽奖页面接口*/
        var objDraw = [];
        $scope.url =$location.search();
        $scope.token=$scope.url['token'];
        $scope.luckView = function () {
            // 抽奖页面接口
            //$.cookie("huiToken", $scope.token)
            $.cookie("huiToken", $scope.token)
            ajaxRquest.getData("/lotteryModule/queryPrizeList", {}).then(function (res) {
                if (res.resCode == '00000') {
                    console.log(res)
                    $scope.$apply(function () {
                        $scope.recordView = res.record;
                        $scope.isEableLottery = res.record.isEableLottery; //是否可以抽奖
                        $scope.records = res.records;
                        if (parseInt($scope.recordView.userCurPoint) < parseInt($scope.recordView.needPoints)) { //积分不小于每次消耗的积分小于为false
                            $scope.insufficientDraw = false;
                        } else {
                            $scope.insufficientDraw = true;
                        }
                        for (var i = 0; i < $scope.records.length; i++) { //获取中奖编号
                            objDraw.push($scope.records[i].giftNo);
                        }
                    })
                } else {
                    if (res.resCode == '00003') {
                        $.cookie("huiToken", null);
                        $scope.formError({
                            text: res.resMsg, //内容
                            resCode: res.resCode
                        })
                    }
                }
            })
        }
        $scope.luckView();
        /*抽奖*/
        $scope.luck = {
            index: -1,	//当前转动到哪个位置，起点位置
            count: 0,	//总共有多少个位置
            timer: 0,	//setTimeout的ID，用clearTimeout清除
            speed: 20,	//初始转动速度
            times: 0,	//转动次数
            cycle: 30,	//转动基本次数：即至少需要转动多少次再进入抽奖环节
            prize: -1,	//中奖位置
            prizeAlertShow: false,  //弹框默认关闭
            init: function (id) {
                if ($("#" + id).find(".luck-unit").length > 0) {       //2.验证id页面奖品数量是否大于0
                    var $luck = $("#" + id);
                    var $units = $luck.find(".luck-unit");         //获取所有奖品
                    this.obj = $luck;                              //获取奖品页面的父级id
                    this.count = $units.length;                    //获取并传入当前奖品数量
                    $luck.find(".luck-unit-" + this.index).addClass("active"); //默认给当前奖品加上选中框
                }
                ;
            },

            roll: function () {                                       //3步
                var index = this.index; //获取当前位置
                var count = this.count; //获取当前奖品共有多少个位置
                var luck = this.obj;
                $(luck).find(".luck-unit-" + index).removeClass("active");   //去除当前奖品的选中框
                index += 1;            //递加index
                if (index > count - 1) {    //判断index是否大于奖品位置数量 如果大于就直为0
                    index = 0;
                }
                ;
                $(luck).find(".luck-unit-" + index).addClass("active");  //递加后为当前奖品增加选中框
                this.index = index;   //传入当前位置
                return false;
            },
            stop: function (index) {
                this.prize = index;
                return false;
            },
            close: function () {
                this.prizeAlertShow = false;
                document.body.style.overflow = "auto";
            }
        };
        //调用抽奖跑马灯
        $scope.roll = function () {
            $scope.luck.times += 1;  //转动次数递加
            $scope.luck.roll();      //调用对象方法
            if ($scope.luck.times > $scope.luck.cycle + 10 && $scope.luck.prize == $scope.luck.index) {
                clearTimeout($scope.luck.timer);
                $scope.luck.prizeAlertShow = true
                document.body.style.overflow = "hidden";
                $scope.luck.prize = -1;
                $scope.luck.times = 0;
                click = false;
                $scope.$apply()
            } else {
                if ($scope.luck.times < $scope.luck.cycle) {  //转动的次数小于转动的基本次数 就以每  10毫秒 加速
                    $scope.luck.speed -= 10;
                }else {//else if ($scope.luck.times == $scope.luck.cycle) {}  转动的次数等于转动的基本次数 开始抽奖
                    if ($scope.luck.times > $scope.luck.cycle + 10 && (($scope.luck.prize == 0 && $scope.luck.index == 7) || $scope.luck.prize == $scope.luck.index + 1)) {
                        $scope.luck.speed += 110;
                    } else {
                        $scope.luck.speed += 20;
                    }
                }
                if ($scope.luck.speed < 40) {
                    $scope.luck.speed = 40;
                }

                $scope.luck.timer = setTimeout($scope.roll, $scope.luck.speed);
            }
            return false;
        }

        $scope.request = function () {  //接口获取数据
            ajaxRquest.getData("/lotteryModule/lottery", {}).then(function (res) {
                console.log(res)
                if (res.resCode == '00000') {
                    $scope.$apply(function () {
                        $scope.record = res.record;
                        //$scope.isEableLottery = res.record.isEableLottery;
                        if($scope.record.userCurTimes=='0'){
                            $scope.zeroOne=$scope.record.userCurTimes
                            $scope.zero();
                        }else{
                            $scope.isEableLottery = res.record.isEableLottery;
                        }

                        if (parseInt($scope.record.userCurPoint) < parseInt($scope.record.point)) { //积分不小于每次消耗的积分小于为false
                            $scope.pointDraw = false;
                        } else {
                            $scope.pointDraw = true;
                        }
                        var index;
                        for (var i = 0; i < objDraw.length; i++) {
                            if ($scope.record.giftNo == objDraw[i]) {
                                index = i; //获取中奖编号信息所对应的号
                            }
                        }
                        console.log(index)
                        $scope.luck.prize = index;//抽奖结果与生成抽奖编号信息相等
                        $scope.roll()
                    })
                } else {
                    if (res.resCode == '00003') {
                        $.cookie("huiToken", null);
                        $scope.formError({
                            text: res.resMsg, //内容
                            resCode: res.resCode
                        })
                    }
                    if (res.resCode == '00001') {  //网络服务器异常处理
                        $scope.formError({
                            text: res.resMsg, //内容
                            resCode: res.resCode
                        })
                        click = false;
                    }
                }
            })
        }
        var zero=2
        $scope.zero=function(){//最后一次为0时避免提前提示不能抽奖
            if($scope.zeroOne=='0'){
                if(zero==2){
                    $scope.isEableLottery='Y'
                }
                zero!=2?$scope.isEableLottery='N':zero=2;
                zero--;
                console.log($scope.isEableLottery)
            }
        }
        //闪灯效果
        var num = 0;
        $(".twinkle").attr("class",function(){
            setInterval(function(){
                num++;
                if(num%2==0){
                    $('#deng').addClass("twinkleTwo");
                }else{
                    $('#deng').addClass("twinkle");
                    $('#deng').removeClass('twinkleTwo');
                }
            },500)
        })
        var click = false;
        $scope.$watch('$viewContentLoaded', function () {
            $scope.luck.init('luck'); //1 .调用并传入抽奖页面 id luck
            $("#btn").click(function () {
                $("#btn").addClass("cjBtnDom");  //按下弹起效果
                setTimeout(function () {
                    $("#btn").removeClass("cjBtnDom");
                }, 200);
                $scope.zero();//最后一次为0时避免提前提示不能抽奖
                if ($scope.isEableLottery == "Y") {

                    if (click) {   //第一次click默认为  false
                        return false;
                    }
                    else {
                        $scope.luck.speed = 100;  //第一次速度传入为100
                        $scope.request()
                        click = true;
                        return false;
                    }
                } else if ($scope.isEableLottery == "N") {
                    $scope.luck.prizeAlertShow = true
                    document.body.style.overflow = "hidden";
                    $scope.$apply()
                } else {
                    return false;
                }
            });
        });
    }]);
